


/**
 * primitive 多边形墙体 大量加载
 * 
 */
import Primitive from "../Primitive"

const DEFAULT_STYLE = {
  color: Cesium.Color.GOLD,
  slices: 128
}

export default class CricleWallPrimitive extends Primitive {
  /**
   * 
   * @param {*} center 
   * @param {*} radius 
   * @param {*} height 
   * @param {*} options 
   */
  constructor(center, radius, height, options = {}) {
    super()
    this._center = center
    this._radius = radius
    this._height = height
    this._style = {
      ...DEFAULT_STYLE,
      ...options
    }
    this._type = 'circleWall_primitive'

    this.delegate = new Cesium.Primitive({
      geometryInstances: new Cesium.GeometryInstance({
        geometry: this.createGeometry()
      }),
      appearance: new Cesium.MaterialAppearance({
        material: Cesium.Material.fromType('Color', {
          color: this._style?.color
        }),
        flat: true
      }),
      asynchronous: false
    })
    this.mounted()
  }
  /**
   * 初始化
   */
  mounted() {
    this.positions = this._positions
    this.style = this._style
    !this.delegate.appearance && this.setAppearance({ type: 'Color' })
  }

  /**
   * 创建图形
   * @returns 
   */
  createGeometry() {
    let positions = this._getPositions(this._radius)
    return new Cesium.WallGeometry({
      positions: positions,
      minimumHeights: this._getHeights(positions.length, 0),
      maximumHeights: this._getHeights(positions.length, this._height)
    })
  }

  /**
   *
   * @param length
   * @param hegiht
   * @returns {*[]}
   * @private
   */
  _getHeights(length, hegiht) {
    let heights = []
    for (let i = 0; i < length; i++) {
      heights.push(hegiht)
    }
    return heights
  }

  /**
   * 设置geometry样式
   * @param {*} style 
   * @returns 
   */
  setStyle(style) {
    if (!style) return
    Object.assign(this._style, { ...style })
    this.delegate.geometryInstances.geometry = this.createGeometry()
  }

  /**
   *
   * @returns {*}
   * @private
   */
  _getPositions() {
    let pnts = []
    let modelMatrix = Cesium.Transforms.eastNorthUpToFixedFrame(
      this._center
    )
    for (let i = 0; i < this._style.slices; i++) {
      let angle = (i / this._style.slices) * Cesium.Math.TWO_PI
      let x = Math.cos(angle)
      let y = Math.sin(angle)
      let point = new Cesium.Cartesian3(
        x * this._radius,
        y * this._radius,
        0.0
      )
      pnts.push(
        Cesium.Matrix4.multiplyByPoint(
          modelMatrix,
          point,
          new Cesium.Cartesian3()
        )
      )
    }
    pnts.push(pnts[0])
    return pnts
  }

  set center(position) {
    this._center = position
    this.delegate.geometryInstances.geometry = this.createGeometry()
    return this
  }

  get center() {
    return this._center
  }

  set radius(radius) {
    this._radius = radius
    this.delegate.geometryInstances.geometry = this.createGeometry()
    return this
  }

  get radius() {
    return this._radius
  }

  set height(height) {
    this._height = height
    this.delegate.geometryInstances.geometry = this.createGeometry()
  }

  get height() {
    return this._height
  }

  /**
   * 设置高度
   * @param {*} height 
   */
  setHeight(height) {
    this._height = height
    this.delegate.geometryInstances.geometry = this.createGeometry()
  }

  /**
   * 设置中心点
   * @param {*} center 
   */
  setCenter(center) {
    this._center = center
    this.delegate.geometryInstances.geometry = this.createGeometry()
  }

  /**
   * 设置中心点
   * @param {*} radius 
   */
  setRadius(radius) {
    this._radius = radius
    this.delegate.geometryInstances.geometry = this.createGeometry()
  }


}
